local jueshuo = fk.CreateSkill{
  name = "lingling__jueshuo",
}

Fk:loadTranslationTable{
  ["lingling__jueshuo"] = "矍铄",
  [":lingling__jueshuo"] = "从第二轮开始，当你使用【杀】/放弃使用【闪】时，若你上轮也使用过【杀】/放弃使用过【闪】，"..
  "你可以随机获得一张武器牌/防具牌，然后你可以使用之。",

  ["#lingling__jueshuo-use"] = "矍铄：你可以使用%arg",
}

jueshuo:addEffect(fk.CardUsing, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(jueshuo.name) and
      data.card.trueName == "slash" and player.room:getBanner("RoundCount") > 1 then
      local room = player.room
      local round_event = room.logic:getCurrentEvent():findParent(GameEvent.Round, true)
      if round_event == nil then return end
      local round_events = room.logic:getEventsByRule(GameEvent.Round, 1, function (e)
        return e.id < round_event.id
      end, 1)
      if #round_events == 0 then return end
      return #round_events[1]:searchEvents(GameEvent.UseCard, 1, function (e)
        local use = e.data
        return use.from == player and use.card.trueName == "slash"
      end) > 0
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = table.filter(table.connect(room.draw_pile, room.discard_pile), function (id)
      local card = Fk:getCardById(id)
      return card.sub_type == Card.SubtypeWeapon and player:canUseTo(card, player)
    end)
    if #cards > 0 then
      local id = table.random(cards)
      room:moveCardTo(id, Card.PlayerHand, player, fk.ReasonJustMove, jueshuo.name, nil, true, player)
      if not table.contains(player:getCardIds("h"), id) or player.dead then return end
      if Fk:getCardById(id).sub_type == Card.SubtypeWeapon then
        room:askToUseRealCard(player, {
          pattern = {id},
          skill_name = jueshuo.name,
          prompt = "#lingling__jueshuo-use:::"..Fk:getCardById(id):toLogString(),
        })
      end
    end
  end,
})

jueshuo:addEffect(fk.AfterAskForCardUse, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(jueshuo.name) and
      data.pattern == "jink" and data.eventData and
      not (data.result and data.result.from == player) and
      player:getMark("lingling__jueshuo_jink-round") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = table.filter(table.connect(room.draw_pile, room.discard_pile), function (id)
      local card = Fk:getCardById(id)
      return card.sub_type == Card.SubtypeArmor and player:canUseTo(card, player)
    end)
    if #cards > 0 then
      local id = table.random(cards)
      room:moveCardTo(id, Card.PlayerHand, player, fk.ReasonJustMove, jueshuo.name, nil, true, player)
      if not table.contains(player:getCardIds("h"), id) or player.dead then return end
      if Fk:getCardById(id).sub_type == Card.SubtypeArmor then
        room:askToUseRealCard(player, {
          pattern = {id},
          skill_name = jueshuo.name,
          prompt = "#lingling__jueshuo-use:::"..Fk:getCardById(id):toLogString(),
        })
      end
    end
  end,

  can_refresh = function (self, event, target, player, data)
    return target == player and data.pattern == "jink" and data.eventData and
      not (data.result and data.result.from == player)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "lingling__jueshuo_jink", 1)
  end
})

jueshuo:addEffect(fk.RoundStart, {
  can_refresh = function (self, event, target, player, data)
    return player:getMark("lingling__jueshuo_jink") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "lingling__jueshuo_jink", 0)
    room:setPlayerMark(player, "lingling__jueshuo_jink-round", 1)
  end,
})

return jueshuo
